//
// Created by 吴锐 on 2022/2/28.
//

#ifndef STUDY03_MYARRAY_HPP
#define STUDY03_MYARRAY_HPP

#endif //STUDY03_MYARRAY_HPP

#pragma once
#include "iostream"

using namespace std;

template<class T>
class MyArray {
private:
    T* pAddress;
    int m_Capacity;
    int m_Size;

public:
    MyArray(int capacity){
        cout << "有参构造" << endl;
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }

    MyArray(const MyArray& arr){
        cout << "拷贝构造" << endl;
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
//        this->pAddress = arr.pAddress;

        this->pAddress = new T[arr.m_Capacity];

        for(int i = 0 ; i < this->m_Size ; i++){
            this->pAddress[i] = arr.pAddress[i];
        }
    }

    MyArray& operator=(const MyArray& arr){
        cout << "opertaror=调用" << endl;
        if(this->pAddress != NULL){
            delete[] this>pAddress;
            this->pAddress = NULL;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }

        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[arr.m_Capacity];
        for(int i = 0 ; i < this->m_Size ; i++){
            this->pAddress[i] = arr.pAddress;
        }
        return *this;
    }

    void pushBack(const T& val){
        if(this->m_Capacity == this->m_Size){
            return;
        }else {
            this->pAddress[this->m_Size] = val;
            this->m_Size++;
        }
    }

    void popBack(){
        if(this->m_Size == 0){
            return;
        }else{
            this->m_Size--;
        }
    }

    T& operator[](int index){
        return this->pAddress[index];
    }

    int getCapacity(){
        return this->m_Capacity;
    }

    int getSize(){
        return this->m_Size;
    }

    ~MyArray(){
        if(this->pAddress != NULL){
            cout << "析构调用" << endl;
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
};